package fr.smile.sandbox.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import fr.smile.sandbox.ProductDAO;

public class ProductListController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		/*
		 * Récupération du contexte de l'application. Permettant par la suite de
		 * récupérer un bean
		 */
		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		ProductDAO myProduct = (ProductDAO) context.getBean("productDAO");
		request.setAttribute("productsList", myProduct.getAllProducts());
		return new ModelAndView("list");
	}

}
